<template>
  <div class="class-edit-page">
    <el-card>
      <div class="edit-title">新增班级</div>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" class="class-form">
        <el-alert type="info" show-icon class="edit-tip" :closable="false">
          说明：年级选择"一年级"，班号填写为"1班"，则班级名称显示：一年级(1)班
        </el-alert>
        <div class="form-row">
          <el-form-item label="年级：" prop="grade">
            <el-select v-model="form.grade" placeholder="年级" style="width: 200px">
              <el-option v-for="item in gradeOptions" :key="item" :label="item" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item label="班号：" prop="classNo">
            <el-input v-model="form.classNo" placeholder="如：1班" />
          </el-form-item>
        </div>
        <div class="form-row">
          <el-form-item label="班级人数：" prop="studentCount">
            <el-input-number v-model="form.studentCount" :min="1" placeholder="班级人数" style="width: 100%" />
          </el-form-item>
        </div>
        <div class="form-actions">
          <el-button type="primary" @click="handleSubmit">确定</el-button>
          <el-button @click="goBack">取消</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { addClass } from '@/api/class';

const router = useRouter();
const gradeOptions = ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级'];
const formRef = ref();
const form = reactive({
  grade: '',
  classNo: '',
  studentCount: 1,
});

const rules = {
  grade: [{ required: true, message: '请选择年级', trigger: 'change' }],
  classNo: [{ required: true, message: '请输入班号', trigger: 'blur' }],
  studentCount: [{ required: true, message: '请输入班级人数', trigger: 'blur' }],
};

function handleSubmit() {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      const data = {
        ...form,
        className: `${form.grade}(${form.classNo})班`
      };
      addClass(data).then(() => {
      ElMessage.success('新增成功');
      goBack();
      });
    }
  });
}

function goBack() {
  router.back();
}
</script>

<style scoped>
.class-edit-page {
  padding: 40px 24px;
}
.edit-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 24px;
}
.class-form {
  background: #fafbfc;
  padding: 24px 16px 8px 16px;
  border-radius: 8px;
}
.form-row {
  display: flex;
  gap: 24px;
}
.form-actions {
  margin-top: 24px;
  text-align: left;
}
.edit-tip {
  margin-bottom: 16px;
}
</style>
